<template>
  <div>
    <MyTable :arr="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ data: item, ink: index }">
        <td>{{ item.id }}</td>
        <td>{{ item.goods_name }}</td>
        <td>¥ {{ item.goods_price }}</td>
        <td>
          <input
            v-if="item.inputVisible"
            class="tag-input form-control"
            type="text"
            v-model="item.inputValue"
            @keyup.enter="send(item)"
            @blur="lose(item)"
            @keyup.esc="esc(item)"
            v-fo
          />
          <button
            v-else
            @click="add(item)"
            class="btn btn-primary btn-sm add-tag"
            yy
          >
            +Tag
          </button>
          <span
            class="badge badge-warning"
            v-for="(obj, index) in item.tags"
            :key="index"
          >
            {{ obj }}
          </span>
        </td>
        <td>
          <button @click="del(index)" class="btn btn-danger btn-sm">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";
export default {
  components: {
    MyTable,
  },
  // url:https://www.escook.cn/api/goods
  data() {
    return {
      list: [],
    };
  },
  async created() {
    let res = await this.$axios({
      url: "https://www.escook.cn/api/goods",
    });
    console.log(res);
    this.list = res.data.data;
  },
  directives: {
    fo: {
      inserted(val) {
        val.focus();
      },
    },
  },
  methods: {
    del(index) {
      this.list.splice(index, 1);
    },
    add(item) {
      item.inputVisible = true;
    },
    send(item) {
      this.list.push(item.inputValue);
    },
    lose(item) {
      item.inputVisible = false;
    },
    esc(item) {
      item.inputValue = "";
    },
  },
};
</script>

<style>
</style>